<?php

namespace app\controller\notify;

use app\constant\CommonConstant;
use app\constant\ModelConstant;
use app\exception\NotifyException;
use app\model\Notify;
use app\model\Orders;
use app\model\ShopsPayment;
use app\queue\CapturePaymentQueue;
use app\queue\CompleteDraftQueue;
use Carbon\Carbon;
use think\facade\Queue;
use think\helper\Arr;
use think\helper\Str;
use think\Request;

class NotifyAsiabill implements NotifyInterface
{
    public function notify(Request $request)
    {
        $params = $request->all();
        if (empty($params)) throw new NotifyException("params is empty");
        $type = $request->param('type', '');
        $data = $request->param('data', []);
        if (empty($type) || empty($data)) throw new NotifyException('type or data empty');
        $remark = Arr::get($data, 'remark', 0);
        $order_id = intval($remark);
        $order = Orders::findOrEmpty($order_id);
        if ($order->isEmpty()) throw new NotifyException('order not found',$order);
        $isChange = false;
        $orderStatus = Arr::get($data, 'orderStatus', '');
        $tradeNo = Arr::get($data, 'tradeNo', '');
        $orderAmount = Arr::get($data, 'orderAmount', '0.00');
        $orderNo = Arr::get($data, 'orderNo', '');
        //交易号缺失
        if (empty($order->trade_no)) {
            $order->trade_no = $tradeNo;
            $isChange = true;
        }
        //商户号缺失
        if (empty($order->order_no)) {
            $order->order_no = Str::startsWith($orderNo, '#') ? $orderNo : sprintf('#%s', $orderNo);
            $isChange = true;
        }
        //订单金额不对的问题,修正
        if ($orderAmount > 0 && $orderAmount != $order->total_present) {
            $order->total_present = $orderAmount;
            $order->tax_present = bcsub($orderAmount, $order->subtotal_present, 2);
            $isChange = true;
        }
        if ($isChange) $order->save();
        if ($order->pay_method != ShopsPayment::PAY_METHOD_ASIABILL) throw new NotifyException('pay method not allowed',$order);
        if ($order->order_status == Orders::ORDER_STATUS_COMPLETED) throw new NotifyException('order has finished',$order);//订单完成
        if ($type == 'transaction.success') {//成功进入队列
            Orders::completed($order, $orderStatus);
        } elseif ($type == 'transaction.fail') {//失败保存失败信息
            $orderInfo = Arr::get($data, 'orderInfo', '');
            $riskInfo = Arr::get($data, 'riskInfo', '');
            $order_status = Arr::get($data, 'orderStatus', '');
            $exception = sprintf('orderInfo: %s,riskInfo: %s,orderStatus: %s', $orderInfo, $riskInfo, $order_status);
            throw new NotifyException($exception,$order);
        }
        return $order;

    }


    public function capture(Request $request, Orders $order)
    {
        $data = $request->all();
        $code = Arr::get($data,'code','0');
        $orderStatus = Arr::get($data,'orderStatus','');
        $isPaySuccess = $code == '0000' && $orderStatus == "success";
        if(!$isPaySuccess) throw new \Exception("pay fail");
        return true;
    }
}